// console.log("thin3");

import thin_render from "./lib/thin_render.mjs";
import thin_pop, { popclose } from "./lib/thin_pop.mjs";
import { after, clearafterqueue } from "./lib/delay_render.mjs";
import thin_ajax from "./lib/thin_ajax.mjs";
import { alert, confirm, dialog } from "./lib/dialog.mjs";
import { datalist } from "./lib/datalist.mjs";
import template_extend from "./lib/ext_template.mjs";
import validate from "./lib/validate.mjs";
import viewmode from "./lib/viewmode.mjs";

function thin(selector, context = document) {
  // console.log("thin3");

  if (selector === undefined) {
    return;
  } else if (typeof selector === "function") {
    return thin.ready(selector);
  } else {
    // function select() {
    let elements;
    if (typeof selector === "string") {
      if (typeof context === "string") {
        elements = document.querySelector(context).querySelectorAll(selector);
      } else {
        elements = context.querySelectorAll(selector);
      }
    } else if (selector.nodeType) {
      elements = [selector];
    } else if (selector.length) {
      elements = selector;
    }
    return { ...fn, ...{ nodelist: elements } };
    // return { ...fn, ...elements };
    // return Object.assign(elements, fn);
  }
}

// import { render } from "./lib/render.mjs";

// thin.render = render;

let fn = {
  render: function (template, data) {
    this.each((node) => {
      if (data !== undefined) {
        node.thin_data = data;
      }
      node.thin_template = template;
      node.thin_render = function () {
        thin_render(node, node.thin_template);
      };
      node.thin_render();
    });
    clearafterqueue();
    return this;
  },
  rerender: function () {
    this.each((node) => {
      thin(node).empty();
      if (node.thin_render) node.thin_render();
    });
    clearafterqueue();
    return this;
  },

  each: function (callback) {
    this.nodelist.forEach((node) => {
      callback(node);
    });
    return this;
  },
  empty: function () {
    this.nodelist.forEach((node) => {
      node.innerHTML = "";
    });
    return this;
  },
  remove: function () {
    this.nodelist.forEach((node) => {
      node.parentNode.removeChild(node);
    });
  },
  attr: function (attribute, value) {
    this.nodelist.forEach((node) => {
      node.setAttribute(attribute, value);
    });
    return this;
  },
  on: function (event, fn) {
    this.each((node) => {
      node.addEventListener(event, fn);
    });
    return this;
  },
  removeClass: function (classname) {
    this.each((node) => {
      node.classList.remove(classname);
    });
    return this;
  },
  addClass: function (classname) {
    this.each((node) => {
      node.classList.add(classname);
    });
    return this;
  },
};

let onready = [];

// 将对象序列化成queryString
thin.serialize = function (obj) {
  let arr = [];
  for (let key in obj) {
    arr.push(
      key +
        "=" +
        (typeof obj[key] == "object" ? JSON.stringify(obj[key]) : obj[key])
    );
  }
  return arr.join("&");
};

thin.ready = function (fn) {
  onready.push(fn);
};

thin.pop = thin_pop;
thin.popclose = popclose;
thin.confirm = confirm;
thin.alert = alert;
thin.dialog = dialog;
thin.after = after;
thin.clearafterqueue = clearafterqueue;
thin.datalist = datalist;
thin.template_extend = template_extend;
thin.validate = validate;
thin.viewmode = viewmode;
thin.uuid = function () {
  let url = URL.createObjectURL(new Blob());
  URL.revokeObjectURL(url);
  return url.substring(url.lastIndexOf("/") + 1);
};
thin.ajax = thin_ajax;

// window load 事件侦听
window.addEventListener("load", (ev) => {
  while (onready.length) {
    onready.shift()();
  }
});

export { thin };
